/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦，本文采用木兰宽松许可证第2版]
 * 
 * https://zhiqim.org/project/zhiqim_kit/zhiqim_git.htm
 *
 * ZhiqimGit is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.git.refer.ref;

import org.zhiqim.git.objer.objectid.ObjectId;
import org.zhiqim.git.refer.Ref;


/**
 * 快捷方式引用，引用指向目标引用
 * @see ObjectIdRef     对象引用，引用指向一个对象
 *
 * @version v1.0.0 @author zhichenggang 2017-3-23 新建与整理
 */
public class SymbolicRef implements Ref
{
    private final String name;
    private final Ref target;

    public SymbolicRef(String name, Ref target)
    {
        this.name = name;
        this.target = target;
    }
    
    public String getName()
    {
        return name;
    }

    /**********************************************************************************/
    //快捷方式
    /**********************************************************************************/
    
    public boolean isSymbolic()
    {
        return true;
    }

    public Ref getTarget()
    {
        return target;
    }
    
    public ObjectIdRef getUltimateTarget()
    {
        Ref dst = getTarget();
        while (dst.isSymbolic()){
            dst = dst.getTarget();
        }
        
        return (ObjectIdRef)dst;
    }

    /**********************************************************************************/
    //标签判断
    /**********************************************************************************/
    
    public boolean isPeeled()
    {
        return getUltimateTarget().isPeeled();
    }
    
    public ObjectId getPeeledObjectId()
    {
        return getUltimateTarget().getPeeledObjectId();
    }
    
    
    /**********************************************************************************/
    //对象&文件
    /**********************************************************************************/
    
    
    public ObjectId getObjectId()
    {
        return getUltimateTarget().getObjectId();
    }

    public boolean isStored()
    {
        return true;
    }
    
    
    /**********************************************************************************/
    //hashCode&equal&toString
    /**********************************************************************************/

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SymbolicRef other = (SymbolicRef) obj;
        if (name == null)
        {
            if (other.name != null)
                return false;
        }
        else if (!name.equals(other.name))
            return false;
        return true;
    }

    @Override
    public String toString()
    {
        StringBuilder r = new StringBuilder();
        r.append("SymbolicRef[");
        Ref cur = this;
        while (cur.isSymbolic())
        {
            r.append(cur.getName());
            r.append(" -> ");
            cur = cur.getTarget();
        }
        r.append(cur.getName());
        r.append('=');
        r.append(ObjectId.toString(cur.getObjectId()));
        r.append("]");
        return r.toString();
    }
}
